<?php
//显示分店名
function m_subname($subname){
    if(preg_match("(店$)",$subname)){
        $str = '（'.$subname.'）';
    }else{
        $str = '（'.$subname.'店）';
    }
    return $subname ? $str : '';
}

//显示打折信息
function m_discount($d){
    if(is_numeric($d) || is_float($d)){
        $str = ' -- <font color="red">'.$d.'折</font>';
    }else{
        $str = ' -- <font color="red">'.$d.'</font>';
    }
    return $d ? $str : '';
}

function menus() 
{
	global $lang, $cats;

	foreach($cats['info'] as $key=>$val) 
	{
		$html .= '<a href="index.php?action=list&amp;catcode='.$val['catcode'].'">'.$val['catname'].'</a> ';
	}
	$html .= "<br/><br/>";
	return $html;
}

function shopcats($catcode){
    
    $html .= "按分类查看<br/>";
    foreach($CAT AS $k=>$v){
    	if($v['module']=='shop' && strlen($k)==2) $html .= "<a href=\"index.php?file=shoplist&amp;catcode=$k\">$v[catname]</a> ";
    }
    $html .= "<br/><br/>";
}



function submodelcats($modelid = 1)
{
	global $CAT;
	$subcat = array();
	foreach($CAT as $id=>$cat)
	{
		if($cat['modelid'] == $modelid) $subcat[$id] = $cat;
	}

	return $subcat;
}

function  m_pages($num, $curr_page, $mpurl, $perpage = 20) 
{ 
	global $lang;
	if($num > $perpage) 
	{ 
		$page = 10; 
		$offset = 2; 
		$pages = ceil($num / $perpage); 
		$from = $curr_page - $offset; 
		$to = $curr_page + $page - $offset - 1; 

		if($page > $pages) 
		{ 
			$from = 1; 
			$to = $pages; 
		} 
		else 
		{ 
			if($from < 1) 
			{
				$to = $curr_page + 1 - $from; 
				$from = 1; 
				if(($to - $from) < $page && ($to - $from) < $pages) 
				{
					$to = $page; 
				}
			}
			elseif($to > $pages)
			{
				$from = $curr_page - $pages + $to; 
				$to = $pages; 
				if(($to - $from) < $page && ($to - $from) < $pages) 
				{
					$from = $pages - $page + 1; 
				}
			}
		}
		//$multipage .= '<a href="'.$mpurl.'1">&lt;&lt;</a>  '; 
                $pagenext = $curr_page + 1;
        if($curr_page<$pages){
		$multipage .= '<a href="'.$mpurl.$pagenext.'">下一页 </a>'; 
        }
		for($i = $from; $i <= $to; $i++) 
		{ 
			if($i != $curr_page) 
			{ 
				$multipage .= '<a href="'.$mpurl.$i.'">['.$i.']</a> '; 
			} 
			else 
			{ 
				$multipage .= '<u><b>['.$i.']</b></u> '; 
			} 
		} 
		//$multipage .= $pages > $page ? "...<a href=\"$mpurl$pages\"> [$pages] &gt;&gt;</a>" : "<a href=\"$mpurl$pages\">&gt;&gt;</a>"; 
	}
	return $multipage; 
}

function m_strip($string)
{
	$string = str_replace(array('&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;', '&'), array(' ', '&', '"', "'", '', '', '', '{<}', '{>}', '', '', '&amp;'), strip_tags($string,'<img><br>'));
	return str_replace(array('{<}', '{>}'), array('&lt;', '&gt;'), $string);
}

function checksubarea($area){
    global $AREA;
    $arealen = strlen($area);
    foreach($AREA AS $k=>$v){
        if((strlen($k)==$arealen+4) && (substr($k,0,$arealen)==$area)) return TRUE;
    }
    return FALSE;
}



function check_wap()
{
    if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0)
    {
        // Check whether the browser/gateway says it accepts WML.
        $br = "WML";
    }else{
            $browser=substr(trim($_SERVER['HTTP_USER_AGENT']),0,4);
            if ($browser=="Noki" || // Nokia phones and emulators
                $browser=="Eric" || // Ericsson WAP phones and emulators
                $browser=="WapI" || // Ericsson WapIDE 2.0
                $browser=="MC21" || // Ericsson MC218
                $browser=="AUR"  || // Ericsson R320
                $browser=="R380" || // Ericsson R380
                $browser=="UP.B" || // UP.Browser
                $browser=="WinW" || // WinWAP browser
                $browser=="UPG1" || //UP.SDK 4.0
                $browser=="upsi" || // another kind of UP.Browser ??
                $browser=="QWAP" || // unknown QWAPPER browser
                $browser=="Jigs" || // unknown JigSaw browser
                $browser=="Java" || // unknown Java based browser
                $browser=="Alca" || // unknown Alcatel-BE3 browser (UP based?)
                $browser=="MITS" || // unknown Mitsubishi browser
                $browser=="MOT-" || // unknown browser (UP based?)
                $browser=="My S" ||//  unknown Ericsson devkit browser ?
                $browser=="WAPJ" || //  Virtual WAPJAG www.wapjag.de
                $browser=="fetc" || //  fetchpage.cgi Perl script from www.wapcab.de
                $browser=="ALAV" || //  yet another unknown UP based browser ?
                $browser=="Wapa" || //  another unknown browser (Web based "Wapalyzer"?)
                $browser=="UCWE" || //  UCWEB
                $browser=="Symb" || //  Symbian
                $browser=="Oper") // Opera
            {
                $br = "WML";
            }else{
                $br = "HTML";
            }
    }

    if($br =="WML"){
        return TRUE;
    }else{
        return FALSE;
    }
}

function array2utf8(&$array) 
{ 
    foreach($array as $key=>$value) 
    { 
        if(!is_array($value)) 
        { 
            //$array[$key]=mb_convert_encoding($value,"UTF-8","GBK"); 
            $array[$key]=utf8($value); 
        }else{ 
            array2utf8($array[$key]); 
        } 
    } 
}


function getunicode($word) {  //必须是从UTF8编码获取
  //获取其字符的内部数组表示，所以本文件应用utf-8编码！   
  if (is_array( $word))   
    $arr = $word;   
  else     
    $arr = str_split($word);    
  //此时，$arr应类似array(228, 189, 160)   
  //定义一个空字符串存储   
  $bin_str = '';   
  //转成数字再转成二进制字符串，最后联合起来。   
  foreach ($arr as $value)   
    $bin_str .= decbin(ord($value));   
  //此时，$bin_str应类似111001001011110110100000,如果是汉字"你"   
  //正则截取   
  $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);   
  //此时， $bin_str应类似0100111101100000,如果是汉字"你"    
  return bindec($bin_str); //返回类似20320， 汉字"你"   
  //return dechex(bindec($bin_str)); //如想返回十六进制4f60，用这句   
} 



// utf8 -> unicode
function utf8_unicode($c) {
  switch(strlen($c)) {
    case 1:
      return ord($c);
    case 2:
      $n = (ord($c[0]) & 0x3f) << 6;
      $n += ord($c[1]) & 0x3f;
      return $n;
    case 3:
      $n = (ord($c[0]) & 0x1f) << 12;
      $n += (ord($c[1]) & 0x3f) << 6;
      $n += ord($c[2]) & 0x3f;
      return $n;
    case 4:
      $n = (ord($c[0]) & 0x0f) << 18;
      $n += (ord($c[1]) & 0x3f) << 12;
      $n += (ord($c[2]) & 0x3f) << 6;
      $n += ord($c[3]) & 0x3f;
      return $n;
  }
}

function utf8($str){
    $str = iconv('gbk', 'utf-8', $str);
    return $str;
}
function gbk($str){
    $str = iconv('utf-8','gbk',$str);
    return $str;
}

function gb2u($str){
    $return = '';
    preg_match_all("/[\x80-\xff]?./",$str,$ar);
    foreach($ar[0] as $v){
      $return .= "&#".utf8_unicode(iconv("GB2312","UTF-8",$v)).";";
    }
    return $return;
}

function err($str,$color='red'){
    //$str = gb2u($str);
    include mtpl('err');
    exit;
}



?>